package com.schillerchina.baselibrary.network.subscriber;

import android.util.Log;

import com.schillerchina.baselibrary.network.config.ResultBean;
import com.schillerchina.baselibrary.utils.LogUtil;
import com.schillerchina.baselibrary.utils.ToastUtil;

import java.net.ConnectException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @author zhangzhiguo
 * @date 2019-08-05 09:39
 * @Description:
 */

public class DefaultSubscriber<T> implements Observer<T> {

    @Override
    public void onError(Throwable e) {
        onFail(e);
    }

    @Override
    public void onComplete() {

    }

    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(T t) {
//        onSuccess(t);
        if (t instanceof ResultBean) {
            if (((ResultBean) t).getStatus_code().equals("200") || ((ResultBean) t).getStatus_code().equals("201")) {
                onSuccess(t);
            } else {
                onFailure(t);
                ToastUtil.showToast(((ResultBean) t).getMessage());
            }
        }
    }

    public void onSuccess(T t) {

    }

    public void onFailure(T t) {

    }

    public void onFail(Throwable e) {
        Log.i("net", "onFail:" + e.toString());
//        if (BuildConfig.DEBUG) {
//            if (e instanceof ConnectException) {
//                ToastUtil.showToast("连接服务器失败");
//            } else {
////                ToastUtil.showToast(e);
//            }
//        }
        LogUtil.e(e);
    }

    public boolean onDealCode(String code) {
        return true;
    }
}
